﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace YouDaoNotesCodes
{
    class XmlDetails
    {
        /// <summary>
        /// 平时能遇到xml的所有操作！！！
        /// </summary>
        public void XmlAnaly()
        {
            try
            {
                #region 加载xml文件
                XmlDocument doc = new XmlDocument();
                doc.Load("../xml/XmlShiTiLei.xml");//bin目录下，如何加载特定目录下的xml呢？Json可以

                doc.Load("E:\\nk.xml");//这样就可以
                #endregion

                #region 加载xml字符串
                string inXml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
                                    + "<users xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
                                    + "<SecurityCode>0001</SecurityCode>"
                                    + "<requesttb>"
                                    + "<code>1307030218</code>"
                                    + "<name>zwq</name>"
                                    + "<birth>0121</birth>"
                                    + "<adress>cdfgb</adress>"
                                    + "</requesttb>"
                                    + "</users>";
                doc.LoadXml(inXml);
                #endregion

                //方式1：常用
                #region 实体类转输出xml
                users users = new users();
                List<RequestTB> list = new List<RequestTB>();

                users.SecurityCode = "0001";

                RequestTB rt = new RequestTB();
                rt.code = "1307030218";
                rt.name = "zwq";
                rt.birth = "0121";
                rt.adress = "cdfgb";

                list.Add(rt);
                users.requesttb = list;
                string outXml = XmlSerializeHelper.XmlSerialize<users>(users);//输出xml
                #endregion

                #region xml转实体类，取出某个节点值看看~
                users = XmlSerializeHelper.DESerializer<users>(outXml);
                foreach (RequestTB tb in users.requesttb)
                {
                    string name = tb.name;
                    //Console.WriteLine(name);
                }
                #endregion

                //方式2：简单，取节点值不明晰
                #region 实体类输出xml
                users users2 = new users();
                RequestTB rt2 = new RequestTB();
                rt2.code = "1307030218";
                rt2.name = "zwq";
                rt2.birth = "0121";
                rt2.adress = "cdfgb";
                users2.requesttb2 = rt2;
                string outXml2 = XmlSerializeHelper.XmlSerialize<users>(users2);
                //Console.Write(outXml2);
                #endregion

                #region xml转实体类，取出某个节点值看看
                users2 = XmlSerializeHelper.DESerializer<users>(outXml2);
                string name2 = users2.requesttb2.name;
                //Console.WriteLine(name2);
                #endregion


                #region xml增加节点
                XmlOperation xo = new XmlOperation();
                xo.Add1();
                #endregion

                #region xml某节点增加属性
                xo.Add1();
                #endregion

                #region xml删除节点
                xo.delete1();
                #endregion

                #region xml删除特性
                xo.delete1();
                #endregion

            }
            catch (Exception e)
            {
                throw e;
            }
        }


    }
    #region XML序列化公共处理类
    /// <summary>
    /// XML序列化公共处理类
    /// </summary>
    public static class XmlSerializeHelper
    {
        #region 将实体对象转换成
        /// <summary>
        /// 将实体对象转换成XML
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="obj">实体对象</param>
        public static string XmlSerialize<T>(T obj)
        {
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                    Type t = obj.GetType();
                    XmlSerializer serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(sw, obj);
                    sw.Close();
                    return sw.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("将实体对象转换成XML异常", ex);
            }
        }
        #endregion

        #region 将XML转换成实体对象
        /// <summary>
        /// 将XML转换成实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="strXML">XML</param>
        public static T DESerializer<T>(string strXML) where T : class
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("将XML转换成实体对象异常", ex);
            }
        }
        #endregion
    }
    #endregion
}
